import { defineStore } from 'pinia';
import {
  reqGetCategory1ListData,
  reqGetCategory2ListData,
  reqGetCategory3ListData,
} from '@/api/product/category';
import type { CategoryStoreModel } from '@/api/product/model/categoryStoreModel';

export const useCategoryListStore = defineStore('categoryList', {
  state: (): CategoryStoreModel => ({
    //1. 一个是id项 用来存储id值，后面数据交互时会用到
    category1Id: undefined,
    category2Id: undefined,
    category3Id: undefined,

    //2. 一个是列表项
    category1List: [],
    category2List: [],
    category3List: [],
  }),

  actions: {
    //1. 获取一级分类数据
    async getCategory1ListData() {
      const result = await reqGetCategory1ListData();
      //直接赋值
      this.category1List = result;
    },
    // 2. 获取二级分类数据
    async getCategory2ListData(category1Id: number) {
      // 2.1 保存1id
      this.category1Id = category1Id;
      // 2.2 调用接口函数发送请求  获取二级分类数据
      const result = await reqGetCategory2ListData(category1Id);
      // 2.3 去存储一下
      this.category2List = result;
      // 2.4 清空2Id与3Id 其实也就是让二级和三级分类不要显示之前的数据
      this.category2Id = undefined;
      this.category3Id = undefined;

      // 2.5 清空category3List数组数据
      this.category3List = [];
    },
    // 3. 获取三级导航分类数据
    async getCategory3ListData(category2Id: number) {
      this.category2Id = category2Id;
      const result = await reqGetCategory3ListData(category2Id);
      this.category3List = result;

      // 清空category3Id
      this.category3Id = undefined;
    },
    // 4. 给category3Id赋值
    setCategory3Id(category3Id: number) {
      this.category3Id = category3Id;
    },

    getters: {},
  },
});
